/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.wizard;

/**
 *
 * @author groep 11
 * @date 2007-2008
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;

public class MaiaDataPanel extends JPanel {

    private javax.swing.JPanel pnlVillage;
    private javax.swing.JLabel welcomeTitle;
    private JPanel contentPanel;
    private JLabel textLabel;
    private JPanel titlePanel;
    private JTextField txtVillage;
    private JLabel lblErrors;

    public MaiaDataPanel() {
        super();

        contentPanel = getContentPanel();
        contentPanel.setBorder(new EmptyBorder(new Insets(10, 10, 0, 0)));

        titlePanel = new javax.swing.JPanel();
        textLabel = new javax.swing.JLabel();

        setLayout(new java.awt.BorderLayout());

        titlePanel.setLayout(new java.awt.BorderLayout());
        titlePanel.setBackground(Color.gray);

        textLabel.setBackground(Color.gray);
        textLabel.setFont(new Font("MS Sans Serif", Font.BOLD, 14));
        textLabel.setText("Game Data");
        textLabel.setBorder(new EmptyBorder(new Insets(10, 10, 10, 10)));
        textLabel.setOpaque(true);

        titlePanel.add(textLabel, BorderLayout.CENTER);

        add(titlePanel, BorderLayout.NORTH);
        JPanel secondaryPanel = new JPanel();
        secondaryPanel.add(contentPanel, BorderLayout.NORTH);
        add(secondaryPanel, BorderLayout.WEST);

    }

    private JPanel getContentPanel() {

        JPanel contentPanel1 = new JPanel();

        welcomeTitle = new javax.swing.JLabel();
        pnlVillage = new javax.swing.JPanel();

        contentPanel1.setLayout(new java.awt.BorderLayout());

        welcomeTitle.setText("Choose a Village Name: ");
        welcomeTitle.setPreferredSize(new Dimension(350, 20));

        pnlVillage.setLayout(new GridLayout(0, 1));
        pnlVillage.setBorder(new EmptyBorder(new Insets(20, 20, 10, 150)));

        JLabel lblVillage = new JLabel("Village Name: ");
        
        txtVillage = new JTextField();
        txtVillage.addFocusListener(new FocusListener() {

            public void focusGained(FocusEvent e) {
                txtVillage.setBackground(Color.white);
            }

            public void focusLost(FocusEvent e) {
                txtVillageCheck();
            }
        });
        
        PlainDocument modelVillage = (PlainDocument) txtVillage.getDocument();
        modelVillage.setDocumentFilter(new MyDocumentFilter(20));
        
        setLblErrors(new JLabel("Please fill in a Village Name."));
        getLblErrors().setForeground(new Color(153, 31, 18));
        getLblErrors().setBorder(new EmptyBorder(0, 0, 0, 0));
        getLblErrors().setVisible(false);
             
        pnlVillage.add(lblVillage);
        pnlVillage.add(Box.createHorizontalStrut(20));
        pnlVillage.add(txtVillage);
        pnlVillage.add(getLblErrors());

        JPanel pnlTop = new JPanel(new BorderLayout());

        pnlTop.add(welcomeTitle, BorderLayout.NORTH);
        pnlTop.add(pnlVillage, BorderLayout.SOUTH);

        contentPanel1.add(pnlTop, java.awt.BorderLayout.NORTH);

        JLabel lblClickNext = new JLabel("Your username and village Name will be verified after clicking next.");
        lblClickNext.setFont(new Font("Dialog",Font.PLAIN,12));      

        contentPanel1.add(lblClickNext, java.awt.BorderLayout.CENTER);

        return contentPanel1;
    }

    private static class MyDocumentFilter extends DocumentFilter {

        private int size;

        public MyDocumentFilter(int size) {
            this.size = size;
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            if (fb.getDocument().getLength() + text.length() > size) {
                return;
            }

            super.replace(fb, offset, length, text, attrs);
        }
    }

    public boolean txtVillageCheck() {

        boolean result = false;
        if (txtVillage.getText().trim().equals("")) {
            txtVillage.setBackground(new Color(153, 31, 18));
            getLblErrors().setVisible(true);
            result = false;
        } else {
            txtVillage.setBackground(Color.WHITE);
            getLblErrors().setVisible(false);
            result = true;
        }
        return result;
    }

    public JTextField getTxtVillage() {
        return txtVillage;
    }

    public void setTxtVillage(JTextField txtVillage) {
        this.txtVillage = txtVillage;
    }

    public JLabel getLblErrors() {
        return lblErrors;
    }

    public void setLblErrors(JLabel lblErrors) {
        this.lblErrors = lblErrors;
    }
}
